// 购物车总数量的本地存储键名
const CART_QUANTITY_KEY = 'cart_quantity';

// 获取购物车总数量
export const getCartQuantity = () => {
	try {
		const quantity = uni.getStorageSync(CART_QUANTITY_KEY);
		return quantity ? parseInt(quantity) : 0;
	} catch (error) {
		console.error('获取购物车数量失败:', error);
		return 0;
	}
};

// 更新购物车总数量
export const updateCartQuantity = (newQuantity) => {
	try {
		uni.setStorageSync(CART_QUANTITY_KEY, newQuantity.toString());
		console.log('购物车总数量已更新为:', newQuantity);
		return true;
	} catch (error) {
		console.error('更新购物车数量失败:', error);
		return false;
	}
};

// 计算购物车总数量
export const calculateCartQuantity = (cartList) => {
	return cartList.reduce((sum, item) => sum + item.quantity, 0);
};